package cn.wsalix.install;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wsalix.admin.entity.SysDict;
import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.admin.service.RoleService;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.utils.Utils;

@Service("adminInstall")
public class AdminInstall implements Install {
	private static final Logger logger = LoggerFactory
			.getLogger(AdminInstall.class);
	@Autowired
	private UserService userService;

	Map<String, SysDict> excepParameters = new HashMap<String, SysDict>();
	Map<String, SysRole> roles = new HashMap<String, SysRole>();
	Map<String, SysMenu> menus = new HashMap<String, SysMenu>();

	@Override
	public void invoke() {
		logger.info("");
		Map<String, List<String>> maps = mapByIniFile("init/init_admin.ini");
		for (String key : maps.keySet()) {
			switch (AdminEnum.findByName(key)) {
			case menu:
				insertMenu(maps.get(key));
				break;
			case role:
				insertRole(maps.get(key));
				break;
			case user:
				//insertUser(maps.get(key));
				break;
			default:
				break;
			}

		}

	}

	@Autowired
	private RoleService roleService;

	public void insertRole(List<String> iniLines) {

		for (String iniLine : iniLines) {
			String[] paraValues = iniLine.split("\\|");
			if (paraValues.length < 1) {
				return;
			}
			String code = paraValues[0].trim();
			SysRole obj = roleService.findByCodeOne(code);
			if (obj == null) {
				obj = new SysRole();
				switch (paraValues.length) {
				case 1:
					obj.setCode(code);
					roleService.add(obj);
					roles.put(code, obj);
					break;
				case 2:
					obj.setCode(code);
					obj.setName(code);
					roleService.add(obj);
					roles.put(code, obj);
					break;
				default:
				}
			}
		}
	}

	@Autowired
	private MenuService menuService;

	public void insertMenu(List<String> iniLines) {
		for (String iniLine : iniLines) {
			String[] paraValues = iniLine.split("\\|");
			if (paraValues.length < 1) {
				return;
			}
			String code = paraValues[0].trim();
			SysMenu menu = menuService.findByCodeOne(code);
			if (menu == null) {
				menu = new SysMenu();
				switch (paraValues.length) {
				case 1:
					menu.setCode(code);
					menuService.add(menu);
					menus.put(code, menu);
					break;
				case 2:
					menu.setCode(code);
					menu.setName(paraValues[1].trim());
					menuService.add(menu);
					menus.put(code, menu);
					break;
				default:
				}
			}
		}
	}

	public static Map<String, List<String>> mapByIniFile(String iniFile) {
		Map<String, List<String>> initLines = new HashMap<String, List<String>>();
		InputStream inCfg = Utils.class.getClassLoader().getResourceAsStream(
				iniFile);
		try {
			List<String> cateLines = IOUtils.readLines(inCfg);
			for (String line : cateLines) {
				if (line.trim().equals("") || line.startsWith("#")) {
					continue;
				}
				String[] paras = line.split("=");
				String codeType = paras[0].trim();
				if (codeType.equals("")) {
					continue;
				}
				List<String> currLst = initLines.get(codeType);
				if (currLst == null) {
					currLst = new ArrayList<String>();
				}
				currLst.add(paras[1].trim());
				initLines.put(codeType, currLst);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return initLines;
	}
}
